package com.googlecode.flair
{
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	public class FlairComponentNonUI extends FlairComponent
	{
		public var property : String = "";
		
		public function FlairComponentNonUI(component:Object, property : String)
		{
			super(component);
			this.property = property;
		}
		
		public override function duplicate () : IFlairComponent {
			var clasz : Class = getDefinitionByName(getQualifiedClassName(component)) as Class;
			var thisClasz : Class = getDefinitionByName(getQualifiedClassName(this)) as Class;
			var object : Object = new clasz();
			var duplicate : IFlairComponent = new FlairComponentNonUI(object, property);
			duplicateProperties(duplicate);
			return duplicate;
		}
		
		public override function removeFromParent() : IFlairComponent {
			if (this.parentComponent.component.hasOwnProperty(property)) {
				var array : Array = this.parentComponent.component[property];
				var index : int = array.indexOf(this.component);
				array.splice(index, 1);
				this.parentComponent.component[property] = array;
			}
			return super.removeFromParent();
		}
		
		public override function addToParent(index : int = -1) : void {
			if (this.parentComponent.component.hasOwnProperty(property)) {
				var array : Array = this.parentComponent.component[property];
				var newArray : Array = [];
				if (index == -1 || index == array.length) {
					newArray = array;
					newArray.push(this.component);
				} else if (index == 0) {
					newArray.push(this.component);
				} else if (index < array.length) {
					for each (var item : Object in array) {
						if (array.indexOf(item) == index) {
							newArray.push(this.component);
						}
						newArray.push(item);
					}
				}
				this.parentComponent.component[property] = newArray;
			}
		}
		
	}
}